package com.jintian.smart.kernel.security.service;

import com.jintian.smart.base.api.security.entity.LoginLogEntity;
import com.jintian.smart.base.api.security.form.LoginLogQueryForm;
import com.jintian.smart.base.api.security.view.LoginLogVO;
import com.jintian.smart.kernel.core.domain.ResponseDTO;
import com.jintian.smart.kernel.core.enumeration.UserTypeEnum;
import com.jintian.smart.base.api.security.constant.LoginLogResultEnum;
import com.jintian.smart.kernel.security.mapper.LoginLogMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.beetl.sql.core.page.PageResult;
import org.springframework.stereotype.Service;

/**
 * 登录日志
 */
@Service
@Slf4j
public class LoginLogService {
    @Resource
    private LoginLogMapper loginLogMapper;

    /**
     * @description 分页查询
     */
    public ResponseDTO<PageResult<LoginLogVO>> queryByPage(LoginLogQueryForm queryForm) {
        PageResult<LoginLogVO> pageResult = loginLogMapper.queryByPage(queryForm);
        return ResponseDTO.ok(pageResult);
    }

    /**
     * @description 添加日志
     */
    public void log(LoginLogEntity loginLogEntity) {
        try {
            loginLogMapper.insert(loginLogEntity);
        } catch (Throwable e) {
            log.error(e.getMessage(), e);
        }
    }


    /**
     * 查询上一个登录记录
     *
     * @description 查询上一个登录记录
     */
    public LoginLogVO queryLastByUserId(Long userId, UserTypeEnum userTypeEnum, LoginLogResultEnum loginLogResultEnum) {
        return loginLogMapper.queryLastByUserId(userId, userTypeEnum.getValue(), loginLogResultEnum.getValue());
    }
}
